package com.cat.dynamicPlanning;

/**
 *   @description https://leetcode.cn/problems/check-if-there-is-a-valid-partition-for-the-array/
 *   @author 曲大人的喵
 *   @create 2025/9/4 21:41
 *   @since JDK17
 */

public class Solution71 {
    public boolean validPartition(int[] nums) {
        int n = nums.length;
        boolean[] dp = new boolean[n + 1];
        dp[0] = true;
        dp[2] = nums[1] == nums[0];
        for (int i = 3; i <= n; i++) {
            if (nums[i - 1] == nums[i - 2]) {
                dp[i] = dp[i] || dp[i - 2];
                if (nums[i - 2] == nums[i - 3]) {
                    dp[i] = dp[i] || dp[i - 3];
                }
            }
            if (nums[i - 3] - nums[i - 2] == nums[i - 2] - nums[i - 1] && nums[i - 2] - nums[i - 1] == -1) {
                dp[i] = dp[i] || dp[i - 3];
            }
        }
        return dp[n];
    }
}
